<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/24
 * Time: 16:40
 */

namespace app\console\model;
use think\Model;
use think\Request;
use think\Db;
class MemberCard extends Model
{
    protected static function init()
    {

        // 写入前
        self::event('before_write', function ($data) {
//            ["member_id"] => string(1) "6"
//            ["shop_id"] => string(2) "15"
//            ["system_card_id"] => string(2) "12"

            $system_card = Db::name('system_card')->find($data->system_card_id);
            //dump($system_card['id']);

//                $system_card_id = Request::instance()->param('system_card_id');
//                $system_card = Db::name('system_card')->find($system_card_id);
//
//                $data->start_time = date('Y-m-d H:i:s',time());
//                $data->title = $system_card['title'];
//                $data->content = $system_card['content'];
//                $data->end_time = date('Y-m-d H:i:s',$system_card['day']*3600*24+time());
//                $data->member_id = Request::instance()->param()['member_id'];
//                $data->create_time = date('Y-m-d H:i:s',time());
//                $data->shop_id = Request::instance()->param()['shop_id'];
//                if($system_card['type'] == 1){
//                    $data->count = $data->minus_count =10000;
//                    $type_name = '年卡';
//                    $data['type'] = 1;
//
//                }else{
//                    $data->count = $system_card['count'];
//                    $data->minus_count = $system_card['count'];
//                    $type_name = '次卡';
//                    $data['type'] = 2;
//
//                }
//
//                if($data['type'] == 2){
//
//                    $data['type'] = 2; //如果是次卡不绑定
//                }else{
//                    if(isset($data->car_number)){
//                        Db::name('member')->where('id',$data->member_id)->setField('car_number',$data->car_number);
//                    }
//
//                }
//                //halt($data);
//                (new \app\index\controller\MemberMessage())->jpushMessage(Request::instance()->param()['member_id'],'恭喜您获得会员卡('.$type_name.')一张',$system_card['content'],3);


        });
    }
    public static function http_request($url, $data = null)
    {

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        halt($output);
        return $output;

    }
}